<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :title="isEdit ? '修改评价等级定义' : '新增评价等级定义'"
    width="800px"
  >
    <el-form :model="form" ref="form" label-width="140px" :rules="rules">
      <el-form-item label="评价等级名称" prop="internalLevelName">
        <el-input v-model="form.internalLevelName" placeholder="请输入评价等级名称" />
      </el-form-item>
      <!-- <el-form-item label="等级对应分值" prop="levelScore">
        <el-input v-model.number="form.levelScore" placeholder="请输入等级对应分值" />
      </el-form-item>-->
      <el-form-item class="mb0" label="评价理由是否必填" prop="necReason">
        <el-radio-group v-model="form.necReason">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSure">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { levelCreate, levelUpdate } from '@/api/satisfaction'
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      visible: false,
      isEdit: false,
      form: {
        internalLevelName: null,
        // levelScore: null,
        necReason: null
      },
      selectData: [],
      rules: {
        internalLevelName: [{ required: true, message: '请输入评价等级名称', trigger: 'blur' }],
        // levelScore: [
        //   { required: true, message: '请输入等级对应分值', trigger: 'blur' },
        //   { type: 'number', message: '等级对应分值请输入数字', trigger: 'blur' }
        // ],
        necReason: [{ required: true, message: '请选择评价理由是否必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    edit(bool, obj) {
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, obj)
    },
    handleSure() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = '',
            mes = ''
          if (this.isEdit) {
            mes = '修改操作成功'
            res = await levelUpdate(this.form)
          } else {
            mes = '新增操作成功'
            res = await levelCreate(this.form)
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
